//
//  LSPersonalInfoViewController.m
//  LaSaCivilAffairsDepartment
//
//  Created by 聂志康 on 2018/12/19.
//  Copyright © 2018 wpkj. All rights reserved.
//

#import "LSPersonalInfoViewController.h"
#import "LSPersonalInfoTextCell.h"
#import "LSPersonalInfoIconImageViewCell.h"
#import "LSUserIconViewController.h"
#import "LSUserInfoInputViewController.h"

@interface LSPersonalInfoViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *personalInfoTableView;
@property (nonatomic, strong) NSArray *tableViewContentArray;
@property (nonatomic, strong) NSArray <NSDictionary *> *postListArray;
@property (nonatomic, strong) NSArray <NSDictionary *> *sexListArray;
@property (nonatomic, strong) NSString *postName;
@property (nonatomic, strong) NSString *postCode;
@property (nonatomic, strong) NSString *sex;
@property (nonatomic, strong) NSString *sexCode;
@end

static NSString *LSPersonalInfoIconImageViewCellID = @"LSPersonalInfoIconImageViewCellID";
static NSString *LSPersonalInfoTextCellID = @"LSPersonalInfoTextCellID";

@implementation LSPersonalInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"个人信息";
    self.postName = @"";
    self.postCode = @"";
    if (![[ValidateUtils validateNull:[UserInfoModel sharedInstance].customerSex] isEqualToString:@""]) {
        if ([[UserInfoModel sharedInstance].customerSex isEqualToString:@"0"]) {
            self.sex = @"男";
        } else if ([[UserInfoModel sharedInstance].customerSex isEqualToString:@"1"]) {
            self.sex = @"女";
        } else {
            self.sex = @"";
        }
    } else {
        self.sex = @"";
    }
    self.sexCode = [UserInfoModel sharedInstance].customerSex;
    [self.view addSubview:self.personalInfoTableView];
    [self getPostList];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (_personalInfoTableView) {
        [_personalInfoTableView reloadData];
    }
}

#pragma mark - 弹窗
- (void)showSelectControllerForTag:(int)tag WithContentArray:(NSArray <NSDictionary *> *)contentArray showCode:(NSString *)showCode requestCode:(NSString *)requestCode {
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    for (int i = 0; i < contentArray.count; i++) {
        [alertC addActionTarget:alertC title:contentArray[i][showCode] color:NAV_COLOR action:^(UIAlertAction *action) {
            if (tag == 0) {
                [self updateUserInfoWithSex:false andShowCode:[ValidateUtils validateNull:contentArray[i][showCode]] andRequestCode:[ValidateUtils validateNull:contentArray[i][requestCode]]];
            } else {
                [self updateUserInfoWithSex:true andShowCode:[ValidateUtils validateNull:contentArray[i][showCode]] andRequestCode:[ValidateUtils validateNull:contentArray[i][requestCode]]];
            }
        }];
    }
    [alertC addCancelActionTarget:alertC color:NAV_COLOR title:@"取消"];
    [self presentViewController:alertC animated:true completion:nil];
}

- (void)updateUserInfoWithSex:(BOOL)isSex andShowCode:(NSString *)showCode andRequestCode:(NSString *)requestCode {
    NSDictionary *param = @{};
    if (isSex) {
        param = @{@"token":[ValidateUtils validateNull:[UserInfoModel sharedInstance].token],@"id":[ValidateUtils validateNull:[UserInfoModel sharedInstance].customerId],@"mobilephone":[UserInfoModel sharedInstance].customerMobile,@"sex":requestCode,@"realname":[UserInfoModel sharedInstance].customerRealName,@"post":[UserInfoModel sharedInstance].customerCategoryId};
    } else {
        param = @{@"token":[ValidateUtils validateNull:[UserInfoModel sharedInstance].token],@"id":[ValidateUtils validateNull:[UserInfoModel sharedInstance].customerId],@"mobilephone":[UserInfoModel sharedInstance].customerMobile,@"sex":[UserInfoModel sharedInstance].customerSex,@"realname":[UserInfoModel sharedInstance].customerRealName,@"post":requestCode};
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:true];
    [NetworkUtils requestNOHandleWithMethod:@"POST" URL:UserUpdate param:param completionHandler:^(id responseObject, NSError *connectionError) {
        [hud hideAnimated:true];
        if (!connectionError) {
            NSNumber *status = [responseObject objectForKey:@"status"];
            if (status.integerValue == 200) {
                if (isSex) {
                    self.sex = [ValidateUtils validateNull:showCode];
                    self.sexCode = [ValidateUtils validateNull:requestCode];
                } else {
                    self.postName = [ValidateUtils validateNull:showCode];
                    self.postCode = [ValidateUtils validateNull:requestCode];
                }
                [self.personalInfoTableView reloadData];
            } else {
                [MBProgressHUD showTipMessageInWindow:[ValidateUtils validateNull:responseObject[@"message"]]];
            }
        } else {
            [MBProgressHUD showTipMessageInWindow:connectionError.localizedDescription];
        }
    }];
}

- (void)getPostList {
    [NetworkUtils requestNOHandleWithMethod:@"POST" URL:OASjzd param:@{@"typeGroupName":@"userDuty",@"token":[ValidateUtils validateNull:[UserInfoModel sharedInstance].token]} completionHandler:^(id responseObject, NSError *connectionError) {
        if (!connectionError) {
            NSLog(@"返回数据----%@",responseObject);
            NSArray *objArray = [responseObject objectForKey:@"obj"];
            if (objArray && objArray.count > 0) {
                NSMutableArray *objArrayM = [[NSMutableArray alloc] initWithArray:objArray];
                self.postListArray = objArrayM.copy;
                for (NSDictionary *postDict in objArrayM) {
                    if ([[UserInfoModel sharedInstance].customerCategoryId isEqualToString:[ValidateUtils validateNull:postDict[@"typecode"]]]) {
                        self.postName = [ValidateUtils validateNull:postDict[@"typename"]];
                        break;
                    }
                }
                [self.personalInfoTableView reloadData];
            }
        } else {
            [MBProgressHUD showTipMessageInWindow:[NSString stringWithFormat:@"%@",connectionError.localizedDescription]];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableViewContentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.row == 0) {
//        LSPersonalInfoIconImageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LSPersonalInfoIconImageViewCellID forIndexPath:indexPath];
//        cell.mainTitle = self.tableViewContentArray[indexPath.row];
//        cell.iconImageView.image = self.iconImage;
//        return cell;
//    } else {
    LSPersonalInfoTextCell *cell = [tableView dequeueReusableCellWithIdentifier:LSPersonalInfoTextCellID forIndexPath:indexPath];
    cell.mainText = self.tableViewContentArray[indexPath.row];
    if (indexPath.row == 0) cell.subtitleText = [ValidateUtils validateNull:[UserInfoModel sharedInstance].customerRealName];
    if (indexPath.row == 1) cell.subtitleText = [ValidateUtils validateNull:[UserInfoModel sharedInstance].customerMobile];
    if (indexPath.row == 2) cell.subtitleText = self.postName;
    if (indexPath.row == 3) cell.subtitleText = self.sex;
    return cell;
//    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
//    if (indexPath.row == 0) {
//        LSUserIconViewController *userIconVC = [[LSUserIconViewController alloc] init];
//        userIconVC.nowIconImage = self.iconImage;
//        [self.navigationController pushViewController:userIconVC animated:true];
//    } else
    if (indexPath.row < 2) {
        LSUserInfoInputViewController *userInfoInputVC = [[LSUserInfoInputViewController alloc] init];
        userInfoInputVC.titleText = self.tableViewContentArray[indexPath.row];
        if (indexPath.row == 0) userInfoInputVC.currentText = [ValidateUtils validateNull:[UserInfoModel sharedInstance].customerRealName];
        if (indexPath.row == 1) userInfoInputVC.currentText = [ValidateUtils validateNull:[UserInfoModel sharedInstance].customerMobile];
        [self.navigationController pushViewController:userInfoInputVC animated:true];
    } else {
        if (indexPath.row == 2) {
            // 选择用户职务
            [self showSelectControllerForTag:0 WithContentArray:self.postListArray showCode:@"typename" requestCode:@"typecode"];
        } else {
            // 选择用户性别
            [self showSelectControllerForTag:1 WithContentArray:self.sexListArray showCode:@"typename" requestCode:@"typecode"];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.row == 0) {
//        return 54;
//    } else {
        return LSPersonalInfoTextCellHeight;
//    }
}

- (UITableView *)personalInfoTableView {
    if (!_personalInfoTableView) {
        _personalInfoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-SafeAreaTopHeight-SafeAreaBottomHeight-49)];
        UIView *tableViewHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 15)];
        tableViewHeaderView.backgroundColor = hexColor(EEEEEE);
        _personalInfoTableView.tableHeaderView = tableViewHeaderView;
        _personalInfoTableView.separatorInset = UIEdgeInsetsZero;
        _personalInfoTableView.tableFooterView = [UIView new];
        _personalInfoTableView.backgroundColor = hexColor(EEEEEE);
        _personalInfoTableView.delegate = self;
        _personalInfoTableView.dataSource = self;
        [_personalInfoTableView registerClass:LSPersonalInfoIconImageViewCell.class forCellReuseIdentifier:LSPersonalInfoIconImageViewCellID];
        [_personalInfoTableView registerClass:LSPersonalInfoTextCell.class forCellReuseIdentifier:LSPersonalInfoTextCellID];
    }
    return _personalInfoTableView;
}

- (NSArray *)tableViewContentArray {
    if (!_tableViewContentArray) {
        _tableViewContentArray = @[@"姓名",@"手机号",@"职务",@"性别"];
    }
    return _tableViewContentArray;
}

- (NSArray<NSDictionary *> *)postListArray {
    if (!_postListArray) {
        _postListArray = [[NSArray alloc] init];
    }
    return _postListArray;
}

- (NSArray<NSDictionary *> *)sexListArray {
    if (!_sexListArray) {
        _sexListArray = @[@{@"typecode":@"0",@"typename":@"男"},@{@"typecode":@"1",@"typename":@"女"}];
    }
    return _sexListArray;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
